package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.ShopTemplate;
import com.fx.service.ShopTemplateService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/shopTemplate")
public class ShopTemplateController extends BaseController {
    @Autowired
    private ShopTemplateService shopTemplateService;
    private static String pageTitle = "店铺模板管理";
    private static String pageUrl = "/shopTemplate";

    @GetMapping()
    String shopTemplate(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "shopTemplate/shopTemplate";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ShopTemplate> shopTemplateList = shopTemplateService.getList(query);
        int total = shopTemplateService.getCount(query);
        return renderPage(total, shopTemplateList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "shopTemplate/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        ShopTemplate shopTemplate = shopTemplateService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("shopTemplate", shopTemplate);
        return "shopTemplate/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(ShopTemplate shopTemplate) {
        String id = shopTemplate.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = shopTemplateService.save(shopTemplate);
        } else {
            result = shopTemplateService.updateById(shopTemplate);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (shopTemplateService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }
    @RequestMapping("/initCombo")
    @ResponseBody
    public List<ShopTemplate> initCombo(){
        Map<String,Object> param = new HashMap<String, Object>();
        return shopTemplateService.getList(param);
    }

}
